package com.select.course.controller;

import com.select.course.entity.LoginUser;
import com.select.course.service.UserTeacherService;
import com.select.course.utils.PageUtils;
import com.select.course.utils.R;
import com.select.course.vo.CoursePageVo;
import com.select.course.vo.CourseVo;
import com.select.course.vo.PageVo;
import com.select.course.vo.TeacherVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RequestMapping("/teacher")
@CrossOrigin
@RestController
@Slf4j
public class TeacherController {

    @Resource
    private UserTeacherService userTeacherService;

    // 获取当前课程信息
    @GetMapping("/getcurri/{id}")
    public R getCurri(@PathVariable Long id) {
        R r = userTeacherService.getCurri(id);

        return r;
    }
    @GetMapping("getallselection/{id}")
    public R getAllSelection(@PathVariable Long id) {
        R r = userTeacherService.getAllSelection(id);
        return r;
    }

    // 添加课程信息
    @PostMapping("/addcurri")
    @PreAuthorize("hasAnyRole('root','teacher')")
    public R addClass(@RequestBody CourseVo courseVo) {

        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.addClass(courseVo);
            log.info("用户{}添加课程成功：{}", instance.getUser().getIdNumber(), courseVo);
            return r;
        } catch (AccessDeniedException exception) {
            log.info("用户{}添加课程失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    //admin/updatetea/
    @PreAuthorize("hasRole('teacher')")
    @PutMapping("updatetea")
    public R updateTeacher(@RequestBody TeacherVo teacherVo) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.updateTeacher(teacherVo);
            log.info("用户{}更新了信息成功：{}", instance.getUser().getIdNumber(), teacherVo);
            return r;
        } catch (AccessDeniedException exception) {
            log.info("用户{}更新自己信息失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @PostMapping("/getselection")
    public R getSelection(@RequestBody CoursePageVo coursePageVo) {
        PageUtils pageUtils = userTeacherService.getSelection(coursePageVo);
        return R.ok().put("data", pageUtils);

    }

    @PostMapping("/currilist")
    public R getCurriList(@RequestBody CoursePageVo coursePageVo) {
        PageUtils pageUtils = userTeacherService.getCurriList(coursePageVo);


        return R.ok().put("data",pageUtils);
    }



    //admin/deletetea/
    @DeleteMapping("deletetea/{userId}")
    @PreAuthorize("hasAnyRole('root')")
    public R deleteTeacher(@PathVariable Long userId) {
        R r = userTeacherService.deleteTeacher(userId);
        return r;
    }

    //admin/addtea
    @PostMapping("addtea")
    @PreAuthorize("hasAnyRole('root')")
    public R addTeacher(@RequestBody TeacherVo teacherVo) {

        R r = userTeacherService.addTeacher(teacherVo);


        return r;
    }

    @PostMapping("tealist")
    @PreAuthorize("hasAnyRole('root')")
    public R getTeaList(@RequestBody PageVo pageVo) {
        PageUtils teaList = userTeacherService.getTeaList(pageVo);

        return R.ok().put("data",teaList);
    }


    //'teacher/getteacherself/' + zhanghu
    @GetMapping("/getteacherself/{idNumber}")
    @PreAuthorize("hasAnyRole('root','teacher','student')")
    public R getTeacher(@PathVariable String idNumber) {
        TeacherVo teacherVo = userTeacherService.getTeacherByIdNumber(idNumber);

        return R.ok().put("data", teacherVo);
    }

    @PostMapping("/login_teacher")
    public R loginTeacher(@RequestBody TeacherVo teacherVo) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.loginTeacher(teacherVo);
            log.info("用户{}登陆成功！", instance.getUser().getIdNumber());
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}登录失败！", instance.getUser().getIdNumber());
        }
        return R.error();

    }

    @GetMapping("/loginout_teacher")
    public R loginoutTeacher() {
        LoginUser instance = LoginUser.getInstance();
        try {
            log.info("用户{}登录成功！", instance.getUser().getIdNumber());
            instance.setAuthorities(null);
            instance = null;
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}登录失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @PutMapping("/updatecurri")
    @PreAuthorize("hasAnyRole('root','teacher')")
    public R updateCurri(@RequestBody CourseVo courseVo) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.updateCurri(courseVo);
            log.info("用户{}更新课程成功！", instance.getUser().getIdNumber());
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}更新课程失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @DeleteMapping("deletecurri/{courseId}")
    @PreAuthorize("hasAnyRole('root','teacher')")
    public R deleteCourse(@PathVariable Long courseId) {
        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.deleteCourse(courseId);
            log.info("用户{}删除课程成功！", instance.getUser().getIdNumber());
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}删除课程失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @GetMapping("getselectionnum/{userId}")
    public R getSelectionNum(@PathVariable String userId) {
        R r = userTeacherService.getSelectionNum(userId);

        return r;

    }

    @PostMapping("importData")
    public R importData(MultipartFile file) {

        LoginUser instance = LoginUser.getInstance();
        try {
            R r = userTeacherService.importData(file);
            int successCount = (Integer) r.get("successCount");
            log.info("用户{}导入{}条教师信息成功！", instance.getUser().getIdNumber(), successCount);
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}导入教师信息失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }


    @PreAuthorize("hasRole('root')")
    @GetMapping(value = "/exportData")
    public R exportData(HttpServletResponse response) {
        LoginUser instance = LoginUser.getInstance();
        try {
            userTeacherService.exportData(response);
            log.info("用户{}导出教师信息成功！", instance.getUser().getIdNumber());
            return R.ok();
        } catch (RuntimeException e) {
            log.error("用户{}导出教师信息失败！", instance.getUser().getIdNumber());
        }
        return R.error();
    }
}
